<template>
  <div class="sys-notice-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <el-form :model="state.queryParams" ref="queryForm" :inline="true">
        <el-form-item :label="$t('message.pageConfiguration.title')">
          <el-input v-model="state.queryParams.title" :placeholder="$t('message.pageConfiguration.title')" clearable />
        </el-form-item>
        <el-form-item :label="$t('message.pageConfiguration.type')">
          <el-select v-model="state.queryParams.type" :placeholder="$t('message.pageConfiguration.type')" clearable>
            <el-option :label="$t('message.pageConfiguration.notice')" :value="1" />
            <el-option :label="$t('message.pageConfiguration.notice1')" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'sysNotice:page'"> {{ $t('message.pageConfiguration.query') }} </el-button>
            <el-button icon="ele-Refresh" @click="resetQuery"> {{$t('message.pageConfiguration.reset')}} </el-button>
          </el-button-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="ele-Plus" @click="openAddNotice" v-auth="'sysNotice:add'"> {{$t('message.pageConfiguration.add')}} </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <el-table :data="state.noticeData" style="width: 100%" v-loading="state.loading" border>
        <el-table-column type="index" :label="$t('message.pageConfiguration.number')" width="55" align="center" />
        <el-table-column prop="title" :label="$t('message.pageConfiguration.title')" header-align="center" show-overflow-tooltip />
        <el-table-column prop="content" :label="$t('message.pageConfiguration.content')" header-align="center" show-overflow-tooltip>
          <template #default="scope"> {{ removeHtml(scope.row.content) }} </template>
        </el-table-column>
        <el-table-column prop="type" :label="$t('message.pageConfiguration.type')" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag v-if="scope.row.type === 1"> {{ $t('message.pageConfiguration.notice') }} </el-tag>
            <el-tag type="warning" v-else> {{ $t('message.pageConfiguration.notice1') }} </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" :label="$t('message.pageConfiguration.createTime')" align="center" show-overflow-tooltip />
        <el-table-column prop="status" :label="$t('message.pageConfiguration.status')" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="info" v-if="scope.row.status === 1"> {{ $t('message.pageConfiguration.published') }} </el-tag>
            <el-tag type="warning" v-else> {{ $t('message.pageConfiguration.unpublished') }} </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="publicUserName" :label="$t('message.pageConfiguration.publisher')" align="center" show-overflow-tooltip />
        <el-table-column prop="publicTime" :label="$t('message.pageConfiguration.releaseTime')" align="center" show-overflow-tooltip />
        <el-table-column :label="$t('message.pageConfiguration.operate')" width="250" fixed="right" align="center" show-overflow-tooltip>
          <template #default="scope">
            <el-button icon="ele-Position" size="small" text type="primary" @click="publicNotice(scope.row)" v-auth="'sysNotice:public'" :disabled="scope.row.status === 1"> {{ $t('message.pageConfiguration.release') }} </el-button>
            <el-button icon="ele-Edit" size="small" text type="primary" @click="openEditNotice(scope.row)" v-auth="'sysNotice:update'" :disabled="scope.row.status === 1"> {{ $t('message.pageConfiguration.edit') }} </el-button>
            <el-button icon="ele-Delete" size="small" text type="danger" @click="delNotice(scope.row)" v-auth="'sysNotice:delete'" :disabled="scope.row.status === 1"> {{ $t('message.pageConfiguration.del') }} </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:currentPage="state.tableParams.page" v-model:page-size="state.tableParams.pageSize" :total="state.tableParams.total" :page-sizes="[10, 20, 50, 100]" small background @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes, prev, pager, next, jumper" />
    </el-card>

    <EditNotice ref="editNoticeRef" :title="state.editNoticeTitle" @handleQuery="handleQuery" />
  </div>
</template>

<script lang="ts" setup name="sysNotice">
import { onMounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import commonFunction from '/@/utils/commonFunction';
import EditNotice from '/@/views/system/notice/component/editNotice.vue';

import { getAPI } from '/@/utils/axios-utils';
import { SysNoticeApi } from '/@/api-services/api';
import { SysNotice } from '/@/api-services/models';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();

const editNoticeRef = ref<InstanceType<typeof EditNotice>>();
const { removeHtml } = commonFunction();
const state = reactive({
	loading: false,
	noticeData: [] as Array<SysNotice>,
	queryParams: {
		title: undefined,
		type: undefined,
	},
	tableParams: {
		page: 1,
		pageSize: 20,
		total: 0 as any,
	},
	editNoticeTitle: '',
});

onMounted(async () => {
	handleQuery();
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	let params = Object.assign(state.queryParams, state.tableParams);
	var res = await getAPI(SysNoticeApi).apiSysNoticePagePost(params);
	state.noticeData = res.data.result?.items ?? [];
	state.tableParams.total = res.data.result?.total;
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.title = undefined;
	state.queryParams.type = undefined;
	handleQuery();
};

// 打开新增页面
const openAddNotice = () => {
	state.editNoticeTitle = t('message.pageConfiguration.addnotice');
	editNoticeRef.value?.openDialog({ type: 1 });
};

// 打开编辑页面
const openEditNotice = (row: any) => {
	state.editNoticeTitle = t('message.pageConfiguration.editnotice');
	editNoticeRef.value?.openDialog(row);
};

// 删除
const delNotice = (row: any) => {
	ElMessageBox.confirm(t('message.messahes.confirmDelNotice') + `：【${row.title}】?`, t('message.messages.hint'), {
		confirmButtonText: t('message.pageConfiguration.confirm'),
		cancelButtonText: t('message.pageConfiguration.cancel'),
		type: 'warning',
	})
		.then(async () => {
			await getAPI(SysNoticeApi).apiSysNoticeDeletePost({ id: row.id });
			handleQuery();
			ElMessage.success(t('message.messages.deleteSuccess'));
		})
		.catch(() => {});
};

// 发布
const publicNotice = (row: any) => {
	ElMessageBox.confirm(t('message.messahes.confirmRelease')+`：【${row.title}】，`, +t('message.messages.revoke')+t('message.messages.hint'), {
		confirmButtonText: t('message.pageConfiguration.confirm'),
		cancelButtonText: t('message.pageConfiguration.cancel'),
		type: 'warning',
	})
		.then(async () => {
			await getAPI(SysNoticeApi).apiSysNoticePublicPost({ id: row.id });
			handleQuery();
			ElMessage.success(t('message.messages.releaseSuccess'));
		})
		.catch(() => {});
};

// 改变页面容量
const handleSizeChange = (val: number) => {
	state.tableParams.pageSize = val;
	handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
	state.tableParams.page = val;
	handleQuery();
};
</script>
